package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.zhusx.core.helper.Lib_SelectPhotoHelper;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Arrays;
import com.zhusx.core.utils._EditTexts;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.CarNameData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;
import lumingweihua.future.cn.lumingweihua.widget.dialog.BottomListDialog;

/**
 * * todo 添加车辆 第二步
 * Created by z1986 on 2018/3/17.
 */

public class AddCar2Activity extends BaseCompatActivity {
    @BindView(R.id.edit_license)
    EditText editLicense;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.edit_weight)
    EditText editWeight;
    @BindView(R.id.iv_driver)
    FrescoImageView ivDriver;
    @BindView(R.id.iv_car)
    FrescoImageView ivCar;
    @BindView(R.id.iv_driver_delete)
    ImageView ivDriverDelete;
    @BindView(R.id.iv_car_delete)
    ImageView ivCarDelete;

    OptionsPickerView startCityPickerView;
    String valueName;
    String valuePhone;
    String valueCode;
    String valueType;
    File driverImageFile;
    File carImageFile;

    LoadData<String> submitData;
    LoadData<List<CarNameData>> carNameData;
    Lib_SelectPhotoHelper helper;
    BottomListDialog dialog;
    int selectType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_add_car_2);
        ButterKnife.bind(this);
        valueName = getIntent().getStringExtra("valueName");
        valuePhone = getIntent().getStringExtra("valuePhone");
        valueCode = getIntent().getStringExtra("valueCode");
        init();
    }

    private void init() {
        submitData = new LoadData<>(LoadData.Api.个人中心我的车辆新增, this);
        submitData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                showToast(result.getData());
                setResult(RESULT_OK);
                finish();
            }
        });
        carNameData = new LoadData<>(LoadData.Api.个人中心新增车辆罐体类型选择列表, this);
        carNameData._setOnLoadingListener(new SimpleHttpListener<List<CarNameData>>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<List<CarNameData>> result) {
                final List<CarNameData> list = result.getData();
                if (startCityPickerView == null) {
                    startCityPickerView = new OptionsPickerView.Builder(AddCar2Activity.this, new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            valueType = String.valueOf(options1 + 1);
                            tvType.setTextColor(Color.BLACK);
                            tvType.setText(String.valueOf(list.get(options1)));
                        }
                    }).build();
                    startCityPickerView.setPicker(list);
                }
                startCityPickerView.show();
            }
        });
        helper = new Lib_SelectPhotoHelper(this);
        dialog = new BottomListDialog(this).setData(_Arrays.asList("相册", "拍照"), new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        helper._startSelectPhoto(AddCar2Activity.this);
                        break;
                    case 1:
                        helper._startTakePhoto(AddCar2Activity.this);
                        break;
                }
                dialog.dismiss();
            }
        });
    }

    @OnClick({R.id.iv_driver, R.id.iv_car, R.id.tv_submit, R.id.tv_type, R.id.iv_driver_delete, R.id.iv_car_delete})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_driver_delete:
                driverImageFile = null;
                ivDriverDelete.setVisibility(View.GONE);
                ivDriver.setImageURI(Uri.parse("res://" + getPackageName() + "/" + R.drawable.iv_tianji));
                break;
            case R.id.iv_car_delete:
                carImageFile = null;
                ivCarDelete.setVisibility(View.GONE);
                ivCar.setImageURI(Uri.parse("res://" + getPackageName() + "/" + R.drawable.iv_tianji));
                break;
            case R.id.iv_driver:
                if (driverImageFile == null) {
                    selectType = 0;
                    dialog.show();
                } else {
                    startActivity(new Intent(v.getContext(), PhotoActivity.class).putExtra(_EXTRA_String, Uri.fromFile(driverImageFile).toString()));
                }
                break;
            case R.id.iv_car:
                if (carImageFile == null) {
                    selectType = 1;
                    dialog.show();
                } else {
                    startActivity(new Intent(v.getContext(), PhotoActivity.class).putExtra(_EXTRA_String, Uri.fromFile(carImageFile).toString()));
                }
                break;
            case R.id.tv_submit:
                String license = editLicense.getText().toString();
                if (TextUtils.isEmpty(license)) {
                    showToast("请输入车牌号");
                    return;
                }
                if (TextUtils.isEmpty(valueType)) {
                    showToast("请选择车辆类型");
                    return;
                }
                String weight = editWeight.getText().toString();
                if (TextUtils.isEmpty(weight)) {
                    showToast("请输入最大载重");
                    return;
                }
                if (driverImageFile == null) {
                    showToast("请选择车辆驾驶证");
                    return;
                }
                if (carImageFile == null) {
                    showToast("请选择车辆行驶证");
                    return;
                }
                submitData._loadData(valueName, valuePhone, valueCode, license, valueType, weight, toBase64String(driverImageFile), toBase64String(carImageFile));
                break;
            case R.id.tv_type:
                showType();
                break;
        }
    }

    private void showType() {
        _EditTexts.hideInputMethod(this);
        if (carNameData._hasCache()) {
            startCityPickerView.show();
        } else {
            carNameData._loadData();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        helper._onActivityResult(requestCode, resultCode, data, new Lib_SelectPhotoHelper.onDataListener() {
            @Override
            public void onPhoto(File file) {
                if (selectType == 0) {
                    driverImageFile = compressFile(file);
                    ivDriver.setImageURI(Uri.fromFile(driverImageFile));
                    ivDriverDelete.setVisibility(View.VISIBLE);
                } else {
                    carImageFile = compressFile(file);
                    ivCar.setImageURI(Uri.fromFile(carImageFile));
                    ivCarDelete.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
